package com.yuwei.order

/**
 * 选择地址页面
 */
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.LogUtils
import com.yuwei.base.RoutePath
import com.yuwei.common.event.BusEvent
import com.yuwei.common.event.LiveDataBus
import com.yuwei.common.mvi.BaseMviActivity
import com.yuwei.common.mvi.UiState
import com.yuwei.database.entity.AddressEntity
import com.yuwei.order.adapter.AddressAdapter
import com.yuwei.order.databinding.ActivityAddressBinding
@Route(path = RoutePath.MODULE_ORDER_ADDRESS_ACTIVITY)
class AddressActivity : BaseMviActivity<ActivityAddressBinding,OrderViewModel>() {
    private val addressAdapter = AddressAdapter()
    override fun successData(it: UiState.onSuccess<*>) {
        when(it.type){
            "getAllAddress"->{
                val list = it.data as List<AddressEntity>
                if(list.isNotEmpty()){
                    binding.rvReceivingAddress.visibility = View.VISIBLE
                    binding.ivBox.visibility = View.GONE
                    binding.tvBox.visibility = View.GONE
                }else{
                    binding.rvReceivingAddress.visibility = View.GONE
                    binding.ivBox.visibility = View.VISIBLE
                    binding.tvBox.visibility = View.VISIBLE
                }
                addressAdapter.data.clear()
                addressAdapter.data.addAll(list)
                addressAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun failureData(error: String) {

    }

    override fun initView() {
        binding.btnAddAddress.setOnClickListener {
            ARouter.getInstance().build(RoutePath.MODULE_ORDER_ADD_ADDRESS_ACTIVITY).withSerializable("addressEntity",null).navigation()
        }
        binding.ivBack.setOnClickListener {
            finish()
        }

        binding.rvReceivingAddress.adapter = addressAdapter
        binding.rvReceivingAddress.layoutManager = LinearLayoutManager(this)
        addressAdapter.addChildClickViewIds(R.id.item_iv_check,R.id.item_iv_edit)
        //选择地址
        addressAdapter.setOnItemChildClickListener { adapter, view, position ->
            when(view.id){
                R.id.item_iv_check->{
                    addressAdapter.data[position].isChecked = !addressAdapter.data[position].isChecked
                    addressAdapter.notifyItemChanged(position)
                    if(addressAdapter.data[position].isChecked){
                        addressAdapter.data.forEach {
                            if(it.id != addressAdapter.data[position].id){
                                it.isChecked = false
                            }
                        }
                        addressAdapter.notifyDataSetChanged()
                    }

                }
                //修改地址
                R.id.item_iv_edit->{
                    ARouter.getInstance().build(RoutePath.MODULE_ORDER_ADD_ADDRESS_ACTIVITY)
                        .withSerializable("addressEntity",addressAdapter.data[position])
                        .navigation()
                }
            }
        }
    }

    override fun initData() {
        addressAdapter.setOnItemClickListener { adapter, view, position ->
            finish()
            //发送通知
            LiveDataBus.getDefault<BusEvent>("address").value =
                BusEvent(addressAdapter.data[position], 0)
        }
    }

    override fun onResume() {
        super.onResume()
        vm.sendIntent(OrderIntent.getAllAddress)
    }

}